package main

import (
	"encoding/json"

	"jlpay-sdk-go/pkg/jlpay/demo"
	"jlpay-sdk-go/pkg/jlpay/models/trans/common"
	servicecommon "jlpay-sdk-go/pkg/jlpay/services/trans/common"
	"jlpay-sdk-go/pkg/jlpay/utils"
)

// 构建撤销请求
func buildCancelRequest() *common.CancelRequest {
	request := common.NewCancelRequest()

	// 设置请求参数
	request.SetMchID("849584358120018")
	request.SetOutTradeNo("ld201911023465529")
	request.SetOriOutTradeNo("ld2019102216465525")
	request.SetOriTransactionID("80011557201909171853011568")
	request.SetTotalFee("1")
	request.SetRemark("测试取消订单")
	request.SetMchCreateIP("127.0.0.1")
	request.SetLongitude("116.397128")
	request.SetLatitude("39.916527")

	return request
}

// 撤销示例
func cancelDemo(cancelService *servicecommon.CancelService) {
	// 创建撤销请求
	request := buildCancelRequest()

	// 序列化请求数据并打印
	requestMap, _ := request.ToMap()
	requestJSON, _ := json.Marshal(requestMap)
	utils.Debugf("撤销请求数据: %s", string(requestJSON))

	// 发送请求
	response, err := cancelService.Cancel(request)
	if err != nil {
		utils.Errorf("撤销请求失败: %v", err)
		return
	}

	// 处理响应 - 使用BaseResponse接口的HandleResponse方法
	response.HandleResponse("取消订单")
}

func main() {
	// 设置日志级别为info，显示详细的调试信息
	utils.SetLogLevel("info")

	utils.Info("=============================")
	utils.Info("===== 取消订单示例 开始 =====")
	utils.Info("=============================")

	// 创建配置对象和撤销服务
	config := demo.CreateDemoConfig()
	cancelService := servicecommon.NewCancelService(config)

	cancelDemo(cancelService)

	utils.Info("=============================")
	utils.Info("===== 取消订单示例执行完毕 =====")
	utils.Info("=============================")
}
